package cc.wsyw126.java.nowcoder;

import java.util.Scanner;

/**
 * Created by junpeng.yang on 17/3/19.
 */
public class MaxValueInArray {

    int MaxSubSum3(int arr[], int len) {
        int i;
        int MaxSum = -100000;
        int CurSum = 0;
        for (i = 0; i < len; i++) {
            CurSum += arr[i];
            if (CurSum > MaxSum)
                MaxSum = CurSum;
            //如果累加和出现小于0的情况，
            //则和最大的子序列肯定不可能包含前面的元素，
            //这时将累加和置0，从下个元素重新开始累加
            if (CurSum < 0)
                CurSum = 0;
        }
        return MaxSum;
    }

    public static void main(String[] args) {
        MaxValueInArray maxValue = new MaxValueInArray();
        int len = 0;
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String next = scanner.nextLine();
            len = Integer.valueOf(next);
            int[] copy = new int[len];
            if (scanner.hasNext()) {
                String s = scanner.nextLine();
                String[] split = s.split(" ");
                for (int i = 0; i < split.length; i++) {
                    copy[i] = Integer.valueOf(split[i]);
                }
                int result = maxValue.MaxSubSum3(copy, len);
                System.out.println(result);
            }
        }

    }
}
